<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class PublicController extends \Think\Controller {

    /**
     * 后台用户登录
     * @author 
     */
    public function login($username = null, $password = null, $verify = null){
        if(IS_POST){
            /* 检测验证码 TODO: 
            if(!check_verify($verify)){
                $this->error('验证码输入错误！');
            }*/

            /* 检测登录 */
            //$username = I("username","");
            //$password = I("password","");

            if (empty($username) or empty($password)) {
            	 $this->error("4011:用户名或密码不能为空！");
            }
            
            //这个是密码加密算法  由函数完成
            $password = password_sha1($password);

            /* 登录用户 */
            $Member = D('Member');

            if($Member->login($username , $password)){ //登录用户
                //TODO:跳转到登录前页面
                $this->success('2001:登录成功！', U('Index/index'));
            } else {
                $this->error($Member->getError());
            }

        } else {
            if(is_login()){
                $this->redirect('Index/index');
            }else{
                /* 读取数据库中的配置 */
                $config	=	S('DB_CONFIG_DATA');
                if(!$config){
                    $config	=	D('Config')->lists();
                    S('DB_CONFIG_DATA',$config);
                }
                C($config); //添加配置
                //dump(session());
                $this->display();
            }
        }
    }

    /* 退出登录 */
    public function logout(){
        if(is_login()){
            D('Member')->logout();
            session('[destroy]');
            $this->success('退出成功！', U('login'));
        } else {
            $this->redirect('login');
        }
    }

    public function verify(){
        $verify = new \Think\Verify();
        $verify->entry(1);
    }
	
	public function getInfo() {
		C('SHOW_PAGE_TRACE' ,false);
        if(!is_login()) {
            echo "document.write('<p class=\"think-user\">[ <a href=\"/Admin/\">登录</a> ]');";
        }else{
			echo "document.write('<p class=\"think-user\">[ <a href=\"/Admin/\">".session('user_auth.username').$_SESSION['loginUserName']."</a><a href=\"/Admin/Public/logout/\">退出</a> ]</p>');";
		}
    }

}
